const { sequelize } = require('./src/config/database');
const UserAiConversation = require('./src/models/UserAiConversation');

async function testRefreshChat() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    const userUuid = 'c381dc8e-64ab-4d26-a4d9-563b52ec26ae';
    
    // 查询用户的所有对话记录
    const conversations = await UserAiConversation.findAll({
      where: {
        user_uuid: userUuid
      },
      attributes: ['id', 'ai_person_id', 'total_messages', 'conversation_data']
    });
    
    console.log(`\n用户 ${userUuid} 的对话记录:`);
    conversations.forEach(conv => {
      const messageCount = conv.conversation_data?.messages?.length || 0;
      console.log(`- AI ${conv.ai_person_id}: ${messageCount} 条消息 (total_messages: ${conv.total_messages})`);
      
      // 显示前几条消息的摘要
      if (conv.conversation_data?.messages?.length > 0) {
        conv.conversation_data.messages.slice(0, 3).forEach((msg, index) => {
          const content = msg.content?.substring(0, 50) || '';
          console.log(`  ${index + 1}. [${msg.sender}] ${content}${content.length >= 50 ? '...' : ''}`);
        });
      }
    });
    
    // 选择一个有多于2条消息的对话进行测试
    const testConversation = conversations.find(conv => 
      (conv.conversation_data?.messages?.length || 0) > 2
    );
    
    if (testConversation) {
      console.log(`\n📝 测试对话 AI ${testConversation.ai_person_id}:`);
      console.log(`原始消息数: ${testConversation.conversation_data.messages.length}`);
      console.log('建议调用刷新接口进行测试...');
    } else {
      console.log('\n⚠️ 没有找到消息数超过2条的对话记录可供测试');
    }
    
    await sequelize.close();
  } catch (error) {
    console.error('测试失败:', error);
  }
}

testRefreshChat(); 